/*Copyright 2014 Rajesh Putta http://www.codesnippets4all.com

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
* 
*/

package com.codesnippets4all.jthunder.core.execution.status;

import java.util.LinkedHashMap;
import java.util.Map;

import com.codesnippets4all.jthunder.core.execution.IStatus;

/*
 * @author Rajesh Putta
 */
public class AutomationStatusManager {

	private IStatusContext phaseStatusContext = new StatusContext();
	
	private Map<String, IStatusContext> contextMap = new LinkedHashMap<String, IStatusContext>();

	public void setPhaseStatusContext(IStatusContext phaseStatusContext) {
		this.phaseStatusContext = phaseStatusContext;
	}
	
	public IStatusContext getPhaseStatusContext() {
		return phaseStatusContext;
	}
	
	public Map<String, IStatusContext> getContextMap() {
		return contextMap;
	}
	
	public void setContext(String name, IStatusContext context)
	{
		this.contextMap.put(name, context);
	}
	
	public IStatusContext getPhaseContext(String name)
	{
		return this.contextMap.get(name);
	}
	
	public IStatus getTaskStatus(String phaseName, String taskName)
	{
		IStatusContext phaseContext= this.contextMap.get(phaseName);
		
		if(phaseContext!=null)
		{
			return phaseContext.getStatus(taskName);
		}
		
		return null;
	}
	
	public Object getTaskResult(String phaseName, String taskName)
	{
		IStatusContext phaseContext= this.contextMap.get(phaseName);
		
		if(phaseContext!=null)
		{
			IStatus status=phaseContext.getStatus(taskName);
			
			return status.getResult();
		}
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getTaskResultOfType(String phaseName, String taskName, Class<T> clazz)
	{
		IStatusContext phaseContext= this.contextMap.get(phaseName);
		
		if(phaseContext!=null)
		{
			IStatus status=phaseContext.getStatus(taskName);
			
			return (T)status.getResult();
		}
		
		return null;
	}
	
	
}
